﻿Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Class AES

    Dim AES As New RijndaelManaged

    Function EncryptString(ByVal Value As String, ByVal _password As String) As String

        Dim md5 As New MD5CryptoServiceProvider
        Dim key() As Byte = md5.ComputeHash(Encoding.UTF8.GetBytes(_password))

        md5.Clear()
        AES.Key = key
        AES.GenerateIV()

        Dim iv() As Byte = AES.IV
        Dim ms As New MemoryStream

        ms.Write(iv, 0, iv.Length)

        Dim cs As New CryptoStream(ms, AES.CreateEncryptor, CryptoStreamMode.Write)
        Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(Value)

        cs.Write(data, 0, data.Length)
        cs.FlushFinalBlock()

        Dim encdata() As Byte = ms.ToArray()
        Return (Convert.ToBase64String(encdata))
        cs.Close()
        AES.Clear()

    End Function

    Function DecryptString(ByVal Value As String, ByVal _password As String) As String

        Try

            Dim rijndaelIvLength As Integer = 16
            Dim md5 As New MD5CryptoServiceProvider
            Dim key() As Byte = md5.ComputeHash(Encoding.UTF8.GetBytes(_password))

            md5.Clear()

            Dim encdata() As Byte = Convert.FromBase64String(Value)
            Dim ms As New MemoryStream(encdata)
            Dim iv(15) As Byte

            ms.Read(iv, 0, rijndaelIvLength)
            AES.IV = iv
            AES.Key = key

            Dim cs As New CryptoStream(ms, AES.CreateDecryptor, CryptoStreamMode.Read)

            Dim data(ms.Length - rijndaelIvLength) As Byte
            Dim i As Integer = cs.Read(data, 0, data.Length)
            cs.Close()
            Return (System.Text.Encoding.UTF8.GetString(data, 0, i))

            AES.Clear()

        Catch ex As Exception
            Throw New FalsePasswordException("Falsches Kennwort!")
        End Try

    End Function




End Class
